package com.cxg.fairy.ec.main.personal.order;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cxg.fairy.ui.recycler.DataConverter;
import com.cxg.fairy.ui.recycler.MultipleFields;
import com.cxg.fairy.ui.recycler.MultipleItemEntity;

import java.math.BigDecimal;
import java.util.ArrayList;

/**
 * Created by CFairy
 */

public class OrderListDataConverter extends DataConverter {

    @Override
    public ArrayList<MultipleItemEntity> convert() {

        final JSONArray array = JSON.parseObject(getJsonData()).getJSONArray("data");
        final int size = array.size();
        for (int i = 0; i < size; i++) {
            final JSONObject data = array.getJSONObject(i);
            final JSONArray thumbArray = data.getJSONArray("thumb");
            final String title = data.getString("title");
            final int id = data.getInteger("id");
            final int total = data.getInteger("total");
            final int extractedStatus = data.getInteger("extractedStatus");
            final int status = data.getInteger("status");
            final BigDecimal amount = data.getBigDecimal("amount");
            final String time = data.getString("createTime");
            final String orderNo = data.getString("orderNo");
            final int orderType = data.getInteger("orderType");

            final MultipleItemEntity entity = MultipleItemEntity.builder()
                    .setItemType(OrderListItemType.ITEM_ORDER_LIST)
                    .setField(MultipleFields.ID, id)
                    .setField(OrderItemFields.ORDERNO, orderNo)
                    .setField(MultipleFields.IMAGE_URL, thumbArray)
                    .setField(MultipleFields.TITLE, title)
                    .setField(OrderItemFields.PRICE, amount)
                    .setField(OrderItemFields.TOTAL, total)
                    .setField(OrderItemFields.ESTATUS, extractedStatus)
                    .setField(OrderItemFields.STATUS, status)
                    .setField(OrderItemFields.ORDERTYPE, orderType)
                    .setField(OrderItemFields.TIME, time)
                    .build();

            ENTITIES.add(entity);
        }

        return ENTITIES;
    }

    @Override
    public MultipleItemEntity convertHeader() {
        return null;
    }
}
